let init = () => {
    initEvent()
    initArticleList()
}

//分页初始化
let pagePlugins = new Page({
        total: null,
        callback: function(index) {
            initArticleList(index)
        }
    })
    //初始化文章列表
let initArticleList = async(page = 0, searchValue) => {
    let classify = $('#forumListClassify').find('li.on').index()

    let getResult = await getArticleList({
        classify,
        page,
        searchValue
    })

    if (getResult.code == 0) {
        renderArticleLists(getResult.forumList, getResult.del)

        //分页数据渲染
        pagePlugins.total = getResult.total
        pagePlugins.renderPageList()
    } else {
        $('.forum-list-wrapper').html(`<li class="list=group-item forum-list-item">暂无数据</li>`)
    }
}


let renderArticleLists = (forumList, del) => {
        let arr = []
        let classTitles = [null, '精华', '分享', '问答', '招聘']

        let delTemplate = id => `<button id="${id}" class="btn btn-danger btn-sm del-btn">删除</button>`

        forumList.forEach((item, index) => {
                    arr.push(
                            `  <li class="list-group-item forum-list-item">
                    <div class="item-desc">
                        <img src="${(item.userInfo && item.userInfo.avatar)?item.userInfo.avatar:'/img/7.jpg'}" alt="">
                        <div class="read-num-wrapper">
                            <span>${item.__v}</span>
                            <span>/</span>
                            <span>${item.readyNum}</span>
                        </div>
                        <div class="forum-share ${item.isAdmin?'admin':''}">${item.isAdmin?'置顶':classTitles[item.classify]}</div>
                        <div class="forum-title">
                            <a href="/api/article/articleDetail/${item._id}">
                                    ${item.title}
                            </a>
                        </div>
                    </div>
                    <div class="item-time">
                        ${item.ratings.length > 0? creatDocument(item.ratings[0]):`<span class="last-forum-time">发布于：${handleTime.distanceTime(item.publishDate)}</span>`}
                        ${del?delTemplate(item._id):''}
                    </div>
                </li>`
        )
    })
    $('.forum-list-wrapper').html(arr)
}

let creatDocument = (ratingsObj)=>{
     return `<img class="last-forum-user" src="${ratingsObj.avatar?ratingsObj.avatar:'/img/bg.jpg'}" alt="">
                        <span class="last-forum-time">${handleTime.distanceTime(ratingsObj.date)}</span>`
}


let initEvent = () => {
    $('#forumListClassify>li').on('click', onChangeForumListClick)
    $('.search-query').on('keyup', onSearchBtnKeyup)
    // $('.forum-list-wrapper').on('click', '.del-btn', onDelArticleClick)
}

//改变文章分类
let onChangeForumListClick = function(){
    let $this = $(this)
    let classify = $this.index()
    $this.addClass('on').siblings('.on').removeClass('on')
    initArticleList()
}

//查询功能
let onSearchBtnKeyup = function(){
    let searchValue = this.value
    initArticleList(0,searchValue)
}

//删除功能

let onDelArticleClick = async function(){
    let _id = this.id
    let delStatus = await delArticle({_id})

    if(delStatus.code == 0){
        initArticleList()
    }
}


init()